// Project: fcounter_14k50
// FileName: fcounter_14k50
#include <p18f14k50.h>
#include <delays.h>
#include <timers.h>
#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config FOSC = HS
#pragma config PLLEN = ON
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config BORV = 30
#pragma config WDTEN = OFF
#pragma config WDTPS = 32768
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config BBSIZ = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTRB = OFF
#define lcd_DB LATB
#define lcd_E LATCbits.LATC7
#define lcd_RS LATCbits.LATC3
#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)
unsigned long FFF;
unsigned long A, TL;
unsigned char N, sw;
char frq0[] = "f High(MHz)";
char frq1[] = "f Low(kHz)";
char frqP[] = "fPrescaler(MHz)";
char Hz[] = "(MHz)";
void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);
void lcd_putf(unsigned long f);
void lcd_putfLow(unsigned long f);
void lcd_putfPre(unsigned long f);
void UserInit(void);
void lcd_Init(void);
void high(void);
void low(void);
#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code
void high(){
if(INTCONbits.TMR0IF){
INTCONbits.TMR0IF = 0;
A++;
}
if(PIR1bits.TMR1IF){
PIR1bits.TMR1IF = 0;
N++;
if(N > 25){
N = 0;
TRISCbits.TRISC4 = 0;
LATCbits.LATC4 = 0;
TL = TMR0L;
if(sw == 0){
FFF = (A*256 + TL)*8;
}
if(sw == 1){
FFF = A*256 + TL;
}
A = 0; TMR0L = 0;
TRISCbits.TRISC4 = 1;
}
TMR1H = 15; TMR1L = 177;
}
}
void low(){
if(INTCON3bits.INT1IF){
INTCON3bits.INT1IF = 0;
if(sw == 0){
sw = 1;
}else{
sw = 0;
}
Delay_20mS;
}
}
void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay10TCYx(20);
lcd_E = 0;
}
void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}
void lcd_clr(void){
lcd_c4(0x01);
}
void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}
void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}
void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}
void lcd_putf(
unsigned long f){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
lcd_putchr(buf[2]);
lcd_putchr(buf[3]);
lcd_putchr('.');
for(i=4; i<10; i++) lcd_putchr(buf[i]);
}
void lcd_putfLow(
unsigned long f){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=2; i<7; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
for(i=7; i<10; i++) lcd_putchr(buf[i]);
}
void lcd_putfPre(
unsigned long f){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=2; i<6; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
for(i=6; i<10; i++) lcd_putchr(buf[i]);
}
void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}
void UserInit(void){
N = 0; A = 0; sw = 0;
}
void main(void){
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b01110111;
ANSEL = 0b00000000;
ANSELH = 0b00000000;
OpenTimer0(
TIMER_INT_ON &
T0_8BIT &
T0_PS_1_8 &
T0_SOURCE_EXT &
T0_EDGE_RISE
);
INTCON2bits.TMR0IP = 1;
INTCONbits.TMR0IF = 0;
OpenTimer1(
TIMER_INT_ON &
T1_8BIT_RW &
T1_SOURCE_EXT &
T1_PS_1_8 &
T1_OSC1EN_OFF &
T1_SYNC_EXT_OFF
);
IPR1bits.TMR1IP = 1;
PIR1bits.TMR1IF = 0;
INTCON3bits.INT1IE = 1;
INTCON3bits.INT1IP = 0;
INTCON3bits.INT1IF = 0;
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
TMR0H = 0; TMR0L = 0;
TMR1L = 10;
UserInit();
lcd_init();
while(1){
if(PORTCbits.RC2 == 0){
if(sw == 0){
T0CON = 0b11110010;
lcd_clr();
lcd_locate(0, 1);
lcd_puts(frq0);
lcd_locate(1, 4);
lcd_putf(FFF);
}
if(sw == 1){
T0CON = 0b11111010;
lcd_clr();
lcd_locate(0, 1);
lcd_puts(frq1);
lcd_locate(1, 4);
lcd_putfLow(FFF);
}
}
if(PORTCbits.RC2){
T0CON = 0b11110010;
lcd_clr();
lcd_locate(0, 1);
lcd_puts(frqP);
lcd_locate(1, 4);
lcd_putfPre(FFF);
}
Delay_100mS;
}
}